class CustomersController < ApplicationController
  # before_action :set_customer, only: [:show, :edit, :update, :destroy]
  load_and_authorize_resource

  respond_to :html, :json

  def index
    @q = Customer.ransack(params[:q])
    @customers = @q.result.page(params[:page])
    respond_with(@customers)
  end

  def show
    @sales = Sale.where(customer_id: @customer.id).page(params[:page])
    respond_with(@customer)
  end

  def new
    @customer = Customer.new
    respond_with(@customer)
  end

  def edit
  end

  def create
    @customer = Customer.new(customer_params)
    @customer.save
    respond_with(@customer)
  end

  def update
    @customer.update(customer_params)
    respond_with(@customer)
  end

  def destroy
    @customer.destroy
    respond_with(@customer)
  end

  private
    def set_customer
      @customer = Customer.find(params[:id])
    end

    def customer_params
      params.require(:customer).permit(:name, :phone, :email, :wechat, :address, :deleted_at, :province_id, :city_id, :district_id, :street,
                                       car_attributes: [:id, :id_card_no, :license_plate_no, :body_no, :engine_no,
                                                        :registered_at, :last_insurance_company, :_destroy])
    end
end
